package com.ruoyi.web.controller.community.owner;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.OwnerVoteInfoForReturn;
import com.ruoyi.community.service.IOwnerVoteService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.OwnerMemberVote;
import com.ruoyi.community.service.IOwnerMemberVoteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 投票详情Controller
 * 
 * @author ruoyi
 * @date 2021-08-24
 */
@RestController
@RequestMapping("/community/owner/voteInfo")
public class OwnerMemberVoteController extends BaseController
{
    @Autowired
    private IOwnerMemberVoteService ownerMemberVoteService;

    @Autowired
    private IOwnerVoteService ownerVoteService;

    /**
     * 查询投票详情列表
     */
    @PreAuthorize("@ss.hasPermi('community:voteInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(OwnerMemberVote ownerMemberVote)
    {
        startPage();
        List<OwnerMemberVote> list = ownerMemberVoteService.selectOwnerMemberVoteList(ownerMemberVote);
        return getDataTable(list);
    }


    /**
     * 导出投票详情列表
     */
    @PreAuthorize("@ss.hasPermi('community:voteInfo:export')")
    @Log(title = "投票详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnerMemberVote ownerMemberVote)
    {
        List<OwnerMemberVote> list = ownerMemberVoteService.selectOwnerMemberVoteList(ownerMemberVote);
        ExcelUtil<OwnerMemberVote> util = new ExcelUtil<OwnerMemberVote>(OwnerMemberVote.class);
        return util.exportExcel(list, "投票详情数据");
    }

    /**
     * 获取投票详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:voteInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(ownerMemberVoteService.selectOwnerMemberVoteById(id));
    }

    /**
     * 获取投票统计数据
     */
    @PreAuthorize("@ss.hasPermi('community:voteInfo:query')")
    @GetMapping("/stats/{voteId}")
    public OwnerVoteInfoForReturn getStats(@PathVariable("voteId") String voteId)
    {
        OwnerVoteInfoForReturn ownerVoteInfoForReturn = new OwnerVoteInfoForReturn();
        ownerVoteInfoForReturn.setVoteId(voteId);
//        ownerVoteInfoForReturn.setOwnerVote(ownerVoteService.selectOwnerVoteById(voteId));
        ownerVoteInfoForReturn.setTotalNum(ownerMemberVoteService.getVoteMemberTotal(voteId));
        ownerVoteInfoForReturn.setFinishedNum(ownerMemberVoteService.getFinishedMember(voteId));
        ownerVoteInfoForReturn.setOptionStats(ownerMemberVoteService.getOptionStats(voteId));
        return ownerVoteInfoForReturn;
    }

    /**
     * 新增投票详情
     */
    @PreAuthorize("@ss.hasPermi('community:voteInfo:add')")
    @Log(title = "投票详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OwnerMemberVote ownerMemberVote)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        ownerMemberVote.setUserId(userId);
        long l = System.currentTimeMillis();
        Date time=new Date(l);
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ownerMemberVote.setSyncTime(time);
        return toAjax(ownerMemberVoteService.insertOwnerMemberVote(ownerMemberVote));
    }

    /**
     * 修改投票详情
     */
    @PreAuthorize("@ss.hasPermi('community:voteInfo:edit')")
    @Log(title = "投票详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OwnerMemberVote ownerMemberVote)
    {
        return toAjax(ownerMemberVoteService.updateOwnerMemberVote(ownerMemberVote));
    }

    /**
     * 删除投票详情
     */
    @PreAuthorize("@ss.hasPermi('community:voteInfo:remove')")
    @Log(title = "投票详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(ownerMemberVoteService.deleteOwnerMemberVoteByIds(ids));
    }
}
